using UnityEngine;
//using UnityEditor;
using System.Collections;
using System.Xml;
using System.IO;
public class SettingScript : MonoBehaviour {
	bool optGUIbool = false;
	public GUISkin mySkin;
	private Rect optGUIrect = new Rect(Screen.width-400,102,395,120);
	public float camSenScroll = 0.09f;
	public  float camSpeedScroll = 2.0f;
	private float camGlobal;
	private string configPath = "";
	
	public bool setBool = false;
	public int setNumber = 0;
	private bool onMusic = true;
	private bool logOut = false;
	void Awake()
	{

		if(Application.platform == RuntimePlatform.OSXEditor)
			configPath = Application.dataPath+"/../";
		if(Application.platform == RuntimePlatform.IPhonePlayer)
			configPath = Application.persistentDataPath+"/";
		var fconfig = configPath+"Config.xml";
		if(!File.Exists(fconfig))
		{
			var doc = new XmlDocument();
			doc.LoadXml("<item>name</item>");
			doc.RemoveAll();
			var writer = new XmlTextWriter(fconfig,null);
			writer.Formatting = Formatting.Indented;
			// Create our root element
			var rootElement = doc.CreateElement("Config");
			doc.AppendChild(rootElement);
			
			var cam = doc.CreateElement("Camera");
			// And finally the level element
			var camsen = doc.CreateAttribute("Sensitive");
			camsen.Value = "0.09";
			var camspeed = doc.CreateAttribute("Speed");
			camspeed.Value = "2";
			
			cam.Attributes.Append(camsen);
			cam.Attributes.Append(camspeed);	
			// Add the profileElement to the rootElement
			rootElement.AppendChild(cam);
			doc.Save(writer);
		}
		StartCoroutine("LoadSettings");
		//optGUIrect = 
	}
	void Update(){
		//var fpsScript = (FPSInputController)GameObject.Find("WalkCamera").GetComponent(typeof(FPSInputController));
		//fpsScript.nav.speed = camSpeedScroll;
	}
	
	IEnumerator LoadSettings()
	{
		
		WWW configXml = new WWW("file://"+configPath+"/Config.xml");
		yield return configXml;
		if (configXml.error != null) 
		{
			Debug.LogError("There is no XML file"+ configPath);
		} 
		else 
		{
			var xml  = new XmlDocument();
			xml.LoadXml(configXml.text);
			var root = xml.DocumentElement;
			for(var i=0;i<root.ChildNodes.Count;i++)
			{
				var thisnode = root.ChildNodes[i];	
				camSenScroll = float.Parse(thisnode.Attributes["Sensitive"].Value);
				camSpeedScroll = float.Parse(thisnode.Attributes["Speed"].Value);
			}
		}
	}
	
	void OptionGUI(int windowID)
	{
		GUILayout.Space(20);
		GUI.Label(new Rect(165,10,80,30),"Camera");
		GUI.Label (new Rect(0,40,100,30),"Sensitive");
		GUILayout.Space(0);
		camSenScroll = GUILayout.HorizontalSlider(camSenScroll, 0.03f, 0.3f);
		GUI.Label (new Rect(310,40,100,30),string.Format("{0:##0.00}",camSenScroll));
		GUI.Label (new Rect(0,78,100,30),"Speed");
		GUILayout.Space(15);
		camSpeedScroll = GUILayout.HorizontalSlider(camSpeedScroll, 2.0f, 10.0f);
		GUI.Label (new Rect(310,78,100,30),string.Format("{0:##0.0}",camSpeedScroll));
		GUILayout.Space(20);
	}
	
	void OnGUI ()
	{
		GUI.skin = mySkin;
		if(CameraSetting.camMode == 0)
		{
			if(!optGUIbool)
			{
				if(GUI.Button(new Rect(Screen.width-80,22, 80, 80), "","Setting"))
				{
					optGUIbool = true;
				}
			}

			else
			{
				if(GUI.Button(new Rect(Screen.width-80, 22, 80, 80),"","Setting"))
				{
					StartCoroutine("SaveOptionSetting");
					setBool = true;
					optGUIbool = false;
				}
			}
			if(optGUIbool)
			{
				optGUIrect = GUI.Window (2, optGUIrect, OptionGUI, "");
				//now adjust to the group. (0,0) is the topleft corner of the group.
				GUI.BeginGroup (new Rect (0,0,100,100));
				
				// End the group we started above. This is very important to remember!
				GUI.EndGroup ();
			}
		}
	}
	IEnumerator SaveOptionSetting()
	{
		WWW configXml = new WWW("file://"+configPath+"/Config.xml");
		yield return configXml;
		if (configXml.error != null) 
		{
			Debug.LogError("There is no XML file");
		} 
		else 
		{
		    var xml  = new XmlDocument();
		    xml.LoadXml(configXml.text);
			xml.RemoveAll();
			
			// Create our root element
			var rootElement = xml.CreateElement("Config");
			xml.AppendChild(rootElement);
			
			var cam = xml.CreateElement("Camera");
			// And finally the level element
			var camsen = xml.CreateAttribute("Sensitive");
			camsen.Value = string.Format("{0:##0.0000}",camSenScroll);
			var camspeed = xml.CreateAttribute("Speed");
			camspeed.Value = string.Format("{0:##0.0000}",camSpeedScroll);
			cam.Attributes.Append(camsen);
			cam.Attributes.Append(camspeed);	
			// Add the profileElement to the rootElement
			rootElement.AppendChild(cam);
		    xml.Save(configPath+"/Config.xml");
		    Debug.Log("Save File Success");
		}
	}

	private bool AllHitTest()
	{
		GUILayer guiL  = Camera.main.GetComponent(typeof(GUILayer)) as GUILayer;
		GUIElement guiE = guiL.HitTest(Input.mousePosition);
		if(guiE != null)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	

}